package com.shangxia.dog.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangxia.dog.base.entity.Admin;
import com.shangxia.dog.base.service.IAdminService;
import com.shangxia.dog.commom.vo.PageResult;
import com.shangxia.dog.commom.vo.ResultVO;
import com.shangxia.dog.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author shangxia
 * @since 2022-02-03
 */
@Slf4j
@Api(tags = "AdminController")
@RestController
@RequestMapping("/user")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "获取用户")
    @PostMapping("/list")
    public ResultVO<Page<User>> listUsersByPage(@RequestBody(required = false) PageResult<User> page) {
        Page<Admin> adminPage = adminService.listUsersByPage(page);
        if (CollectionUtils.isEmpty(adminPage.getRecords())) {
            return ResultVO.success(new ArrayList<>());
        }
        List<User> users = adminPage.getRecords().stream().map(e -> {
            User user0 = new User();
            e.setPassword(null);
            BeanUtils.copyProperties(e, user0);
            user0.setCreateTimeStr();
            return user0;
        }).collect(Collectors.toList());
        Page<User> result = new Page<>();
        BeanUtils.copyProperties(adminPage, result, "records");
        result.setRecords(users);
        return ResultVO.success(result);
    }

    @ApiOperation(value = "添加用户")
    @PutMapping("")
    public ResultVO<String> putUser(@RequestBody User user) {
        Assert.notNull(user, "请输入参数 user");
        Admin admin = new Admin();
        BeanUtils.copyProperties(user, admin);
        adminService.addAdmin(admin);
        return ResultVO.success("添加成功");
    }

    @ApiOperation(value = "修改状态")
    @PutMapping("/id/{id}/enabled/{enabled}")
    public ResultVO<String> editUserEnable(@PathVariable Long id, @PathVariable Boolean enabled) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setEnabled(enabled);
        adminService.updateUser(admin);
        return ResultVO.success("修改成功");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public ResultVO<String> deleteUser(@PathVariable Long id) {
        adminService.deleteUser(id);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("")
    public ResultVO<String> updateUser(@RequestBody User user) {
        Assert.notNull(user, "请输入参数 user");
        Assert.notNull(user.getId(), "请输入参数 user");
        Admin admin = new Admin();
        BeanUtils.copyProperties(user, admin);
        adminService.updateUser(admin);
        return ResultVO.success("添加成功");
    }


}
